package py.com.alfa.escriba.web.interceptor;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.annotations.interception.ServerInterceptor;
import org.jboss.resteasy.core.ResourceMethod;
import org.jboss.resteasy.core.ServerResponse;
import org.jboss.resteasy.spi.Failure;
import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.interception.PreProcessInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;

import py.com.alfa.escriba.config.EscribaApplicationContextProvider;
import py.com.alfa.escriba.config.HttpHeaderConfig;



@Provider
@ServerInterceptor
public class InvocationContextInterceptor implements PreProcessInterceptor {
	private Logger log = LoggerFactory.getLogger(getClass());

	@Override
	public ServerResponse preProcess(HttpRequest request, ResourceMethod method)
			throws Failure, WebApplicationException {
		Long idEscribano=null;
		String userPrincipal =null;
		Long idUserPrincipal =null;
	    try{
	      if (log.isTraceEnabled())
	        log.trace("executing context interceptor");
	      
	      ApplicationContext ctx = EscribaApplicationContextProvider.getContext();
	      
	      HttpHeaderConfig config = (HttpHeaderConfig) ctx.getBean(BeanNames.HTTP_HEADERS_BEAN);

	      try {
	    	  userPrincipal = request.getHttpHeaders().getRequestHeader(config.getUserPrincipalHeader()).get(0);
	      } catch (Exception e) {
	    	  log.trace("No se encontro header user-principal");
	      }

	      try {
	    	  String idescribano = request.getHttpHeaders().getRequestHeader(config.getIdEscribanoHeader()).get(0);
	    	  idEscribano = Long.parseLong(idescribano);
	      } catch (Exception e) {
	    	  log.trace("No se encontro header id-escribano");
	      }
	      
	      try {
	    	  String iduserprincipal = request.getHttpHeaders().getRequestHeader(config.getIdUserPrincipalHeader()).get(0);
	    	  idUserPrincipal = Long.parseLong(iduserprincipal);
	      } catch (Exception e) {
	    	  log.trace("No se encontro header id-user-principal");
	      }

	      DefaultInvocationContext ic = new DefaultInvocationContext(userPrincipal, idEscribano, idUserPrincipal);

	      request.setAttribute(config.getContextAttributeName(), ic);

	      if (log.isTraceEnabled())
	        log.trace("invocation context set to {}",ic);
	      
	    } catch(Exception e ) {
		  log.error(e.getMessage(), e);
		  log.error("invalid request. cannot get header parameters for invocation context");
		  return (ServerResponse) Response.status(Status.BAD_REQUEST).build();
		}
	    
	    return null;
	}
 
  /*@Override
  public void filter(ContainerRequestContext request) throws IOException {
	String idEscribano=null;
	String userPrincipal =null;
    try{
      if (log.isTraceEnabled())
        log.trace("executing context interceptor");
      
      ApplicationContext ctx = EscribaApplicationContextProvider.getContext();
      
      HttpHeaderConfig config = (HttpHeaderConfig) ctx.getBean(BeanNames.HTTP_HEADERS_BEAN);

      try {
    	  userPrincipal = request.getHeaders().getFirst(config.getUserPrincipalHeader());
      } catch (Exception e) {
    	  log.trace("No se encontro header user-principal");
      }

      try {
    	  idEscribano = request.getHeaders().getFirst(config.getIdEscribanoHeader());
      } catch (Exception e) {
    	  log.trace("No se encontro header id-escribano");
      }

      DefaultInvocationContext ic = new DefaultInvocationContext(userPrincipal, idEscribano);

      request.setProperty(config.getContextAttributeName(), ic);

      if (log.isTraceEnabled())
        log.trace("invocation context set to {}",ic);
      
    } catch(Exception e ) {
      log.error(e.getMessage(), e);
      log.error("invalid request. cannot get header parameters for invocation context");
      throw new IOException(e.getMessage(), e);
    }
  }*/

}
